<?php
/**
*
* @author Blackpawn blackpawn666@hotmail.com - http://blackpawnsarchives.net
* @co_author GameCharmer admin@gamecharmer.com - http://gaming.gamecharmer.com
* @package Age of Dragons Mod :: ( Game Console Required )
* @version $Id: viewking.php 1.1.1 12-26-2009 Blackpawn
* @copyright (c) 2009 Blackpawns Archives
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/
/**
* @ignore
*/

define('GC_MOD', true);
define('IN_PHPBB', true);
define('AOD', true);

$phpbb_root_path =  '../../';
$templates_path = '../../../games/aod/templates/';
$phpEx = substr(strrchr(__FILE__, '.'), 1);

include($phpbb_root_path . 'common.' . $phpEx);
include($phpbb_root_path . 'includes/functions_display.' . $phpEx);
include($phpbb_root_path . 'includes/mods/game_console/gc_common.' . $phpEx);
include($phpbb_root_path . 'games/aod/common.' . $phpEx);

// Start session management
$user->session_begin();
$auth->acl($user->data);
$user->setup();

// Set custom language path
$user->set_custom_lang_path($phpbb_root_path.'games/aod/language/');

//load our language file
$user->add_lang('common');

// Now add the phpBB language path
$user->set_custom_lang_path($phpbb_root_path . 'language');

// Load the Game Console language file
$user->add_lang('mods/game_console');

if ($user->data['user_id'] == ANONYMOUS)
{
	login_box('', $user->lang['LOGIN']);
}

if ($user->data['is_bot'])
{
	redirect(append_sid("{$phpbb_root_path}index.$phpEx"));
}

// Default Vars
$error_msgs = null;
$admin = 0;
$levelup_report = null;
$outcome = FALSE;
$plunder = 0.10;
$capture = 0.05;
$sponsor_sig = null;
$bash_penalty = 0;

// Var Requests
$cmd = request_var('cmd','');
$enemy_id = request_var('uid', 0);
$mode = request_var('mode', 0);
$amount = request_var('amount', 0);

// AOD Config
$kingdoms_per_page = $aod_config['kingdoms_per_page'];
$sponsor_def_bonus = $aod_config['sponsor_def_bonus'];

// Gather some user info
$user_id = $user->data['user_id'];
$account_status = check_account_status($user_id);
if ($account_status == FALSE)
{
	redirect(append_sid("{$phpbb_root_path}/games/index.$phpEx"));
}

// Make sure data is current for both kingdoms
set_kingdom_stats($user_id);
set_kingdom_stats($enemy_id);


// Fetch kingdom data
$king = fetch_kingdom_data($user_id);
$kingdom_status 				= $king['kingdom_status'];
$admin_access 					= $king['admin_access'];
$kingdom_name 					= $king['kingdom_name'];
$kingdom_sponsor 				= $king['kingdom_sponsor'];
$kingdom_level 					= $king['kingdom_level'];
$kingdom_exp					= $king['kingdom_exp'];				
$kingdom_net 					= $king['kingdom_net'];
$kingdom_land 					= fetch_total_land($user_id);
$kingdom_gold 					= $king['kingdom_gold'];
$kingdom_food 					= $king['kingdom_food'];
$kingdom_wood 					= $king['kingdom_wood'];
$kingdom_iron 					= $king['kingdom_iron'];
$kingdom_off 					= $king['kingdom_off'];
$kingdom_turns 					= $king['kingdom_turns'];
$kingdom_spies 					= $king['kingdom_spies'];

// Check to see if this user is a admin staff member
if ($user_id == 2 || $admin_access == 1)
{
	$admin = 1;
}

// Check kingdom status to see if this account is frozen
if($kingdom_status == 1)
{
	// Lets make sure the main admin can't be blocked
	if ($user_id <> 2){
		trigger_error($user->lang['FROZEN'] . back_link());
	}
}

// Fetch enemy kingdom data
$eking = fetch_kingdom_data($enemy_id);
$enemy_status 				= $eking['kingdom_status'];
$enemy_name 				= $eking['kingdom_name'];
$enemy_ruler 				= $eking['kingdom_ruler'];
$enemy_sponsor 				= $eking['kingdom_sponsor'];
$enemy_level 				= $eking['kingdom_level'];
$enemy_pop 					= $eking['kingdom_pop'];
$enemy_wins 				= $eking['kingdom_wins'];
$enemy_losses 				= $eking['kingdom_losses'];
$enemy_net 					= $eking['kingdom_net'];
$enemy_land 				= fetch_total_land($enemy_id);
$enemy_gold 				= $eking['kingdom_gold'];
$enemy_food 				= $eking['kingdom_food'];
$enemy_wood 				= $eking['kingdom_wood'];
$enemy_iron 				= $eking['kingdom_iron'];
$enemy_def 					= $eking['kingdom_def'];
$enemy_turns 				= $eking['kingdom_turns'];
$enemy_villagers 			= $eking['kingdom_villagers'];
$enemy_soldiers 			= $eking['kingdom_soldiers'];
$enemy_spies 				= $eking['kingdom_spies'];
$enemy_macemen 				= $eking['kingdom_macemen'];
$enemy_axemen				= $eking['kingdom_axemen'];
$enemy_spearmen 			= $eking['kingdom_spearmen'];
$enemy_swordsmen 			= $eking['kingdom_swordsmen'];
$enemy_legionnaires 		= $eking['kingdom_legionnaires'];
$enemy_knights 				= $eking['kingdom_knights'];
$enemy_slingers				= $eking['kingdom_slingers'];
$enemy_bowmen 				= $eking['kingdom_bowmen'];
$enemy_long_bowmen 			= $eking['kingdom_long_bowmen'];
$enemy_crossbowmen 			= $eking['kingdom_crossbowmen'];
$enemy_light_sword_cavalry 	= $eking['kingdom_light_sword_cavalry'];
$enemy_heavy_sword_cavalry 	= $eking['kingdom_heavy_sword_cavalry'];
$enemy_light_archer_cavalry = $eking['kingdom_light_archer_cavalry'];
$enemy_heavy_archer_cavalry = $eking['kingdom_heavy_archer_cavalry'];
$enemy_paladin 				= $eking['kingdom_paladin'];
$enemy_dragon_knights 		= $eking['kingdom_dragon_knights'];
$enemy_rams 				= $eking['kingdom_rams'];
$enemy_catapults 			= $eking['kingdom_catapults'];
$enemy_heavy_catapults 		= $eking['kingdom_heavy_catapults'];
$enemy_ballistas 			= $eking['kingdom_ballistas'];
$enemy_trebuchets 			= $eking['kingdom_trebuchets'];
$enemy_chimeras				= $eking['kingdom_chimeras'];
$enemy_hydras 				= $eking['kingdom_hydras'];
$enemy_basilisks 			= $eking['kingdom_basilisks'];
$enemy_wyverns 				= $eking['kingdom_wyverns'];
$enemy_leviathans 			= $eking['kingdom_leviathans'];
$enemy_dragons 				= $eking['kingdom_dragons'];
$enemy_farms 				= $eking['kingdom_farms'];
$enemy_timber_mills 		= $eking['kingdom_timber_mills'];
$enemy_iron_mines 			= $eking['kingdom_iron_mines'];
$enemy_gold_mines 			= $eking['kingdom_gold_mines'];
$enemy_towers 				= $eking['kingdom_towers'];
$enemy_granaries 			= $eking['kingdom_granaries'];
$enemy_woodsheds 			= $eking['kingdom_woodsheds'];
$enemy_ironpits 			= $eking['kingdom_ironpits'];
$enemy_vaults 				= $eking['kingdom_vaults'];

if($enemy_sponsor)
{
	$sponsor_sig = ' <img src="images/sponsor.png" alt="'.$user->lang['GAME_SPONSOR'].'" title="'.$user->lang['GAME_SPONSOR'].'" border="0">';
	$enemy_def = floor($enemy_def + ($enemy_def + $sponsor_def_bonus));
}

if(empty($mode))
{
	// Setup enemy avatar
	$sql = "SELECT * 
	FROM " . USERS_TABLE . "
	WHERE user_id = $enemy_id";
	$result = $db->sql_query($sql);
	$enemy = $db->sql_fetchrow($result);
	$db->sql_freeresult($result);
	
	if (!$enemy)
	{
		trigger_error('NO_USER');
	}
	
	$enemy_user_avatar = $enemy['user_avatar'];
	
	// If user avatar is null force -1 so we get the right image back from the get_user_avatar() function
	if(empty($enemy_user_avatar))
	{
		$enemy_user_avatar = -1;
		
	}
			
	$enemy_avatar = get_user_avatar($enemy_user_avatar, $enemy['user_avatar_type'], 95, 95);
	
	// As long as the user ID doesn't match the enemies ID the user will see the attack & spy links
	if($user_id != $enemy_id)
	{
		$command_buttons = '[<a href="viewking.php?cmd=att&uid='.$enemy_id.'" class="stext">'.$user->lang['ATTACK_KINGDOM'].'</a> ]&nbsp;';
		$command_buttons .= '[<a href="viewking.php?mode=1&uid='.$enemy_id.'" class="stext">'.$user->lang['SPY_ON_KINGDOM'].'</a> ]';
	}
	
	if($enemy_status == 1)
	{
		$command_buttons = '<span class="xtextblue"><b>['.$user->lang['FROZEN'].']</b></span>';
	}
	
}

// Commands
if($cmd == 'att')
{
	if($enemy_net < 1000)
	{
		$error = TRUE;
		$error_msgs .= $user->lang['NET_TO_LOW'];
	}
			
	if($enemy_status == 1)
	{
		$error = TRUE;
		$error_msgs .= $user->lang['FROZEN'];
	}
	
	if($kingdom_off < 1)
	{
		$error = TRUE;
		$error_msgs .= $user->lang['NO_OFF'];
	}

	if($kingdom_turns < 1)
	{
		$error = TRUE;
		$error_msgs .= $user->lang['NO_TURNS'];
	}
		

	$exp_gain = calc_exp($enemy_level, $kingdom_level);
	
	// Try to prevent users from bashing 
	//--------------------------------------------------------------------------------------------------------
	$times_greater = ($aod_config['times_greater']) ? $aod_config['times_greater'] : 3;
	$land_greater = ($aod_config['land_greater']) ? $aod_config['land_greater'] : 3;
	$net_greater = ($aod_config['net_greater']) ? $aod_config['net_greater'] : 4;
	$levels_greater = ($aod_config['levels_greater']) ? $aod_config['levels_greater'] : 10;
	$exp_for_bash = ($aod_config['exp_for_bash']) ? $aod_config['exp_for_bash'] : 1;
	
	// Check to see how much stronger then enemy
	if(( $kingdom_off / $times_greater ) > $enemy_def)
	{
		$exp_gain = $exp_for_bash;
		$bash_penalty = ($aod_config['bash_penalty'] / 100);
	}
		
	// Check to see how much bigger then enemy
	if(( $kingdom_land / $land_greater ) > $enemy_land)
	{
		$exp_gain = $exp_for_bash;
		$bash_penalty = ($aod_config['bash_penalty'] / 100);
	}	
	
	// Check to see how much more networth then the enemy
	if(( $kingdom_net / $net_greater ) > $enemy_net)
	{
	  $exp_gain = $exp_for_bash;
	  $bash_penalty = ($aod_config['bash_penalty'] / 100);
	}
		
	// Check to see if the user is move advanced
	if(($kingdom_level - $levels_greater) > $enemy_level)
	{
		$exp_gain = $exp_for_bash;
		$bash_penalty = ($aod_config['bash_penalty'] / 100);
	}
	
	//--------------------------------------------------------------------------------------------------------
	
	$max_gold_to_plunder = floor($enemy_gold * $plunder);
	$max_gold_after_bash_penalty = floor($max_gold_to_plunder * $bash_penalty);
	$gold_plundered = ($max_gold_to_plunder - $max_gold_after_bash_penalty);
	
	$max_food_to_plunder = floor($enemy_food * $plunder);
	$max_food_after_bash_penalty = floor($max_food_to_plunder * $bash_penalty);
	$food_plundered = ($max_food_to_plunder - $max_food_after_bash_penalty);
	
	$max_wood_to_plunder = floor($enemy_wood * $plunder);
	$max_wood_after_bash_penalty = floor($max_wood_to_plunder * $bash_penalty);
	$wood_plundered = ($max_wood_to_plunder - $max_wood_after_bash_penalty);
	
	$max_iron_to_plunder = floor($enemy_iron * $plunder);
	$max_iron_after_bash_penalty = floor($max_iron_to_plunder * $bash_penalty);
	$iron_plundered = ($max_iron_to_plunder - $max_iron_after_bash_penalty);
	
	$max_villager_capture = floor($enemy_villagers * $capture);
	$max_villager_after_bash_penalty = floor($max_villager_capture * $bash_penalty);
	$villagers_captured = ($max_villager_capture - $max_villager_after_bash_penalty);
	
	if($kingdom_off > $enemy_def){
		$outcome = TRUE;
	}
	
	if(!$error)
	{
	
		if($outcome == TRUE)
		{
			// Successfull Attack
			$exp_needed = exp_to_next_level($kingdom_level);
			
		    if(($kingdom_exp + $exp_gain) >= $exp_needed)
			{			
				$db->sql_query(
				"UPDATE " . AOD_KINGDOMS_TABLE ." SET 
				kingdom_level = kingdom_level + 1,
				kingdom_turns = kingdom_turns_max,
				kingdom_stamina = kingdom_stamina_max,
				kingdom_coins = kingdom_coins + 5
				WHERE user_id = $user_id");
				$db->sql_return_on_error(false);
				
				$levelup_report = sprintf($user->lang['LEVELUP_REPORT']);			
			}

			if($gold_plundered < 1){$gold_plundered = 0;}
			if($food_plundered < 1){$food_plundered = 0;}
			if($wood_plundered < 1){$wood_plundered = 0;}
			if($iron_plundered < 1){$iron_plundered = 0;}
			if($villagers_captured < 1){$villagers_captured = 0;}
			if($exp_gain < 1){$exp_gain = 1;}

			if($gold_plundered == 1){
				$king_plunder_msg[1] = '<span class="ntextred">'.$gold_plundered.'</span> '.$user->lang['UNIT_OF_GOLD'];
				$enemy_plunder_msg[1] = $king_plunder_msg[1];
			}elseif($gold_plundered > 1){
				$king_plunder_msg[1] = '<span class="ntextred">'.number_format($gold_plundered).'</span> '.$user->lang['UNITS_OF_GOLD'];
				$enemy_plunder_msg[1] = $king_plunder_msg[1];
			}
			
			
			if($food_plundered == 1){
				$king_plunder_msg[2] = '<span class="ntextred">'.$food_plundered.'</span> '.$user->lang['UNIT_OF_FOOD'];
				$enemy_plunder_msg[2] = $king_plunder_msg[2];
			}elseif($food_plundered > 1){
				$king_plunder_msg[2] = '<span class="ntextred">'.number_format($food_plundered).'</span> '.$user->lang['UNITS_OF_FOOD'];
				$enemy_plunder_msg[2] = $king_plunder_msg[2];
			}
			
			
			if($wood_plundered == 1){
				$king_plunder_msg[3] = '<span class="ntextred">'.$wood_plundered.'</span> '.$user->lang['UNIT_OF_WOOD'];
				$enemy_plunder_msg[3] = $king_plunder_msg[3];
			}elseif($wood_plundered > 1){
				$king_plunder_msg[3] = '<span class="ntextred">'.number_format($wood_plundered).'</span> '.$user->lang['UNITS_OF_WOOD'];
				$enemy_plunder_msg[3] = $king_plunder_msg[3];
			}
			
			
			if($iron_plundered == 1){
				$king_plunder_msg[4] = '<span class="ntextred">'.$iron_plundered.'</span> '.$user->lang['UNIT_OF_IRON'];
				$enemy_plunder_msg[4] = $king_plunder_msg[4];
			}elseif($iron_plundered > 1){
				$king_plunder_msg[4] = '<span class="ntextred">'.number_format($iron_plundered).'</span> '.$user->lang['UNITS_OF_IRON'];
				$enemy_plunder_msg[4] = $king_plunder_msg[4];
			}
			
			
			if($villagers_captured == 1){
				$king_plunder_msg[5] = sprintf($user->lang['WE_CAPTURED_VILLAGER'],$villagers_captured);
				$enemy_plunder_msg[5] = sprintf($user->lang['WE_LOST_VILLAGER'],$villagers_captured);
			}elseif($villagers_captured > 1){
				$king_plunder_msg[5] = sprintf($user->lang['WE_CAPTURED_VILLAGERS'],number_format($villagers_captured));
				$enemy_plunder_msg[5] = sprintf($user->lang['WE_LOST_VILLAGERS'],number_format($villagers_captured));
			}
			
			$kmsg = build_list($king_plunder_msg);
			$emsg = build_list($enemy_plunder_msg);
			
				
			// Update kingdom data
			$db->sql_query(
			"UPDATE " . AOD_KINGDOMS_TABLE ." SET 
			kingdom_gold = kingdom_gold + $gold_plundered,
			kingdom_food = kingdom_food + $food_plundered,
			kingdom_wood = kingdom_wood + $wood_plundered,
			kingdom_iron = kingdom_iron + $iron_plundered,
			kingdom_villagers = kingdom_villagers + $villagers_captured,
			kingdom_wins = kingdom_wins + 1,
			kingdom_turns = kingdom_turns - 1,
			kingdom_exp = kingdom_exp + $exp_gain
			WHERE user_id = $user_id");
			$db->sql_return_on_error(false);

			// Build kingdom news report
			$kingdom_report = sprintf($user->lang['ATTACK_VICTORY'],$enemy_name).$kmsg;
			
			// Log kingdom news report
			report_kingdom_news($user_id , $kingdom_report , $date);
			
			// Update enemy kingdom data
			$db->sql_query(
			"UPDATE " . AOD_KINGDOMS_TABLE ." SET 
			kingdom_gold = kingdom_gold - $gold_plundered,
			kingdom_food = kingdom_food - $food_plundered,
			kingdom_wood = kingdom_wood - $wood_plundered,
			kingdom_iron = kingdom_iron - $iron_plundered,
			kingdom_villagers = kingdom_villagers - $villagers_captured,
			kingdom_losses = kingdom_losses + 1
			WHERE user_id = $enemy_id");
			$db->sql_return_on_error(false);	
			
			// Build enemy kingdom news report
			$enemy_report = sprintf($user->lang['DEFEND_FAILURE'],$kingdom_name).$emsg;
			
			// Log enemy kingdom news report
			report_kingdom_news($enemy_id , $enemy_report , $date);
									
			// Build the report
			$report = $levelup_report.sprintf($user->lang['SUCCESSFUL_ATTACK'],$enemy_name).$emsg;
			
			// Log the report in the database
			report_transaction($user_id,$report,'viewking.php?uid='.$enemy_id ,$date);
			
			// Now it is time to redirect the user to see the report page so they can see the report
			redirect(append_sid("{$phpbb_root_path}/games/aod/report.$phpEx"));		
		
		}else{

			// Failed Attack

			// Make the kingdom give 50% of their current resources for failing
			$gold_tribute = floor($kingdom_gold / 2);
			$food_tribute = floor($kingdom_food / 2);
			$wood_tribute = floor($kingdom_wood / 2);
			$iron_tribute = floor($kingdom_iron / 2);

			if($gold_tribute < 1){$gold_tribute = 0;}
			if($food_tribute < 1){$food_tribute = 0;}
			if($wood_tribute < 1){$wood_tribute = 0;}
			if($iron_tribute < 1){$iron_tribute = 0;}
			
			if($gold_tribute <= 1){
				$king_tribute_msg[1] = '<span class="ntextred">'.$gold_tribute.'</span> '.$user->lang['UNIT_OF_GOLD'];
				$enemy_tribute_msg[1] = $king_tribute_msg[1];
			}elseif($gold_tribute > 1){
				$king_tribute_msg[1] = '<span class="ntextred">'.number_format($gold_tribute).'</span> '.$user->lang['UNITS_OF_GOLD'];
				$enemy_tribute_msg[1] = $king_tribute_msg[1];
			}

			if($food_tribute <= 1){
				$king_tribute_msg[2] = '<span class="ntextred">'.$food_tribute.'</span> '.$user->lang['UNIT_OF_FOOD'];
				$enemy_tribute_msg[2] = $king_tribute_msg[2];
			}elseif($food_tribute > 1){
				$king_tribute_msg[2] = '<span class="ntextred">'.number_format($food_tribute).'</span> '.$user->lang['UNITS_OF_FOOD'];
				$enemy_tribute_msg[2] = $king_tribute_msg[2];
			}

			if($wood_tribute <= 1){
				$king_tribute_msg[3] = '<span class="ntextred">'.$wood_tribute.'</span> '.$user->lang['UNIT_OF_WOOD'];
				$enemy_tribute_msg[3] = $king_tribute_msg[3];
			}elseif($wood_tribute > 1){
				$king_tribute_msg[3] = '<span class="ntextred">'.number_format($wood_tribute).'</span> '.$user->lang['UNITS_OF_WOOD'];
				$enemy_tribute_msg[3] = $king_tribute_msg[3];
			}

			if($iron_tribute <= 1){
				$king_tribute_msg[4] = '<span class="ntextred">'.$iron_tribute.'</span> '.$user->lang['UNIT_OF_IRON'];
				$enemy_tribute_msg[4] = $king_tribute_msg[4];
			}elseif($iron_tribute > 1){
				$king_tribute_msg[4] = '<span class="ntextred">'.number_format($iron_tribute).'</span> '.$user->lang['UNITS_OF_IRON'];
				$enemy_tribute_msg[4] = $king_tribute_msg[4];
			}
			

			$kmsg = build_list($king_tribute_msg);
			$emsg = build_list($enemy_tribute_msg);
				
			// Update kingdom data
			$db->sql_query(
			"UPDATE " . AOD_KINGDOMS_TABLE ." SET 
			kingdom_gold = kingdom_gold - $gold_tribute,
			kingdom_food = kingdom_food - $food_tribute,
			kingdom_wood = kingdom_wood - $wood_tribute,
			kingdom_iron = kingdom_iron - $iron_tribute,
			kingdom_losses = kingdom_losses + 1,
			kingdom_turns = kingdom_turns - 1
			WHERE user_id = $user_id");
			$db->sql_return_on_error(false);

			// Build kingdom news report
			$kingdom_report = sprintf($user->lang['ATTACK_FAILURE'],$enemy_name).$kmsg;
			
			// Log kingdom news report
			report_kingdom_news($user_id , $kingdom_report , $date);
					
			// Update enemy kingdom data
			$db->sql_query(
			"UPDATE " . AOD_KINGDOMS_TABLE ." SET 
			kingdom_gold = kingdom_gold + $gold_tribute,
			kingdom_food = kingdom_food + $food_tribute,
			kingdom_wood = kingdom_wood + $wood_tribute,
			kingdom_iron = kingdom_iron + $iron_tribute,
			kingdom_wins = kingdom_wins + 1
			WHERE user_id = $enemy_id");
			$db->sql_return_on_error(false);		
			
			// Build enemy kingdom news report
			$enemy_report = sprintf($user->lang['DEFEND_VICTORY'],$kingdom_name).$emsg;
			
			// Log enemy kingdom news report
			report_kingdom_news($enemy_id , $enemy_report , $date);
						
			// Build the report
			$report = sprintf($user->lang['UNSUCCESSFUL_ATTACK'],$enemy_name).$kmsg;
			
			// Log the report in the database
			report_transaction($user_id,$report,'viewking.php?uid='.$enemy_id ,$date);
			
			// Now it is time to redirect the user to see the report page so they can see the report
			redirect(append_sid("{$phpbb_root_path}/games/aod/report.$phpEx"));			
			
		}	
	}				
}

if($cmd == $user->lang['SEND_SPIES'])
{
	if($enemy_net < 1000)
	{
		$error = TRUE;
		$error_msgs .= $user->lang['NET_TO_LOW'];
	}
		
	if($amount < 1)
	{
		$error = TRUE;
		$error_msgs .= $user->lang['NO_SPIES_SENT'];
	}

	if($amount > $kingdom_spies)
	{
		$error = TRUE;
		$error_msgs .= $user->lang['NOT_ENOUGH_SPIES'];
	}	
	
	if($kingdom_turns < 1)
	{
		$error = TRUE;
		$error_msgs .= $user->lang['NO_TURNS'];
	}
		
	if(!$error)
	{
		$spy_def = (($enemy_towers * 5) + ($enemy_spies * 2));
		
		if($amount > $spy_def)
		{
			$outcome = TRUE;
		}
		
		if($spy_def >= $amount)
		{
			$spies_kia = $amount;		
		}else{
			$spies_kia = $spy_def;
		}
		
		
		
		if($outcome == TRUE)
		{
			// Success
			$spy_report = '
			<table border="0" cellspacing="0" cellpadding="4" align="center" width="65%" class="game_panel">
				<tr>
					<td colspan="2" class="game_header" align="center" width="100%"><span class="ntext"><b>'.$user->lang['SPY_REPORT'].'</b></span></td>
				</tr>';
			
			$spy_report .= '<tr><td class="game_row3" align="right" style="border-right:solid 1px #000000;" width="45%"><span class="stext">'.$user->lang['KINGDOM'].' :  </span></td><td class="game_row2" align="left" style="border-bottom:solid 1px #000000;" width="55%"><span class="stext">'.$enemy_name.' </span></td></tr>';
			$spy_report .= '<tr><td class="game_row3" align="right" style="border-right:solid 1px #000000;"><span class="stext">'.$user->lang['LEVEL'].' :  </span></td><td class="game_row2" align="left" style="border-bottom:solid 1px #000000;"><span class="stext">'.number_format($enemy_level).' </span></td></tr>';
			$spy_report .= '<tr><td class="game_row3" align="right" style="border-right:solid 1px #000000;"><span class="stext">'.$user->lang['POPULATION'].' :  </span></td><td class="game_row2" align="left" style="border-bottom:solid 1px #000000;"><span class="stext">'.number_format($enemy_pop).' </span></td></tr>';
			$spy_report .= '<tr><td class="game_row3" align="right" style="border-right:solid 1px #000000;"><span class="stext">'.$user->lang['LAND'].' :  </span></td><td class="game_row2" align="left" style="border-bottom:solid 1px #000000;"><span class="stext">'.number_format($enemy_land).' </span></td></tr>';
			$spy_report .= '<tr><td class="game_row3" align="right" style="border-right:solid 1px #000000;"><span class="stext">'.$user->lang['GOLD'].' :  </span></td><td class="game_row2" align="left" style="border-bottom:solid 1px #000000;"><span class="stext">'.number_format($enemy_gold).' </span></td></tr>';
			$spy_report .= '<tr><td class="game_row3" align="right" style="border-right:solid 1px #000000;"><span class="stext">'.$user->lang['FOOD'].' :  </span></td><td class="game_row2" align="left" style="border-bottom:solid 1px #000000;"><span class="stext">'.number_format($enemy_food).' </span></td></tr>';
			$spy_report .= '<tr><td class="game_row3" align="right" style="border-right:solid 1px #000000;"><span class="stext">'.$user->lang['WOOD'].' :  </span></td><td class="game_row2" align="left" style="border-bottom:solid 1px #000000;"><span class="stext">'.number_format($enemy_wood).' </span></td></tr>';
			$spy_report .= '<tr><td class="game_row3" align="right" style="border-right:solid 1px #000000;"><span class="stext">'.$user->lang['IRON'].' :  </span></td><td class="game_row2" align="left" style="border-bottom:solid 1px #000000;"><span class="stext">'.number_format($enemy_iron).' </span></td></tr>';
			$spy_report .= '<tr><td class="game_row3" align="right" style="border-right:solid 1px #000000;"><span class="stext">'.$user->lang['DEFENSE'].' :  </span></td><td class="game_row2" align="left" style="border-bottom:solid 1px #000000;"><span class="stext">'.number_format($enemy_def).' </span></td></tr>';
			

			// Build unit list	
			$unitcount = fetch_kingdom_unit_counts($enemy_id);		
			$sql = "SELECT *
			FROM " . AOD_UNITS_TABLE . "		
			ORDER BY id ASC";
			$result = $db->sql_query($sql);
						
			while ($row = $db->sql_fetchrow($result))
			{
				$uid = $row['id'];
				$unit_name = $row['unit_name'];
				$spy_report .= '<tr><td class="game_row3" align="right" style="border-right:solid 1px #000000;"><span class="stext">'.$unit_name.' : </span></td><td class="game_row2" align="left" style="border-bottom:solid 1px #000000;"><span class="stext">'.number_format($unitcount[$uid]).' </span></td></tr>';
			}	
			$db->sql_freeresult($result);
				
			// Build unit list	
			$buildingcount = fetch_kingdom_building_counts($enemy_id);
			$sql = "SELECT *
			FROM " . AOD_BUILDINGS_TABLE . "	
			ORDER BY id ASC";
			$result = $db->sql_query($sql);
						
			while ($row = $db->sql_fetchrow($result))
			{
				$bid = $row['id'];
				$building_name = $row['building_name'];
		
				$spy_report .= '<tr><td class="game_row3" align="right" style="border-right:solid 1px #000000;"><span class="stext">'.$building_name.' : </span></td><td class="game_row2" align="left" style="border-bottom:solid 1px #000000;"><span class="stext">'.number_format($buildingcount[$bid]).' </span></td></tr>';
			}	
			$db->sql_freeresult($result);
			$spy_report .= '</table><br />';


			// Update kingdom data
			$db->sql_query(
			"UPDATE " . AOD_KINGDOMS_TABLE ." SET 
			kingdom_spies = kingdom_spies - $spies_kia,
			kingdom_turns = kingdom_turns - 1
			WHERE user_id = $user_id");
			$db->sql_return_on_error(false);

			if($spies_kia == 1)
			{
			
				$spy_killed_msg = $user->lang['SPY_KILLED'];
				$kingdom_report = sprintf($user->lang['SPY_SUCCESS_REPORT'],$enemy_name,$spy_killed_msg).$spy_report;
				$report = sprintf($user->lang['SUCCESSFUL_SPY_MISSION'],$enemy_name,$spy_killed_msg).$spy_report;
				report_kingdom_news($user_id , $kingdom_report , $date);
				report_transaction($user_id,$report,'viewking.php?uid='.$enemy_id ,$date);
				
			}else{
				
				$spy_killed_msg = sprintf($user->lang['SPIES_KILLED'],$spies_kia);
				$kingdom_report = sprintf($user->lang['SPY_SUCCESS_REPORT'],$enemy_name,$spy_killed_msg).$spy_report;
				$report = sprintf($user->lang['SUCCESSFUL_SPY_MISSION'],$enemy_name,$spy_killed_msg).$spy_report;
				report_kingdom_news($user_id , $kingdom_report , $date);
				report_transaction($user_id,$report,'viewking.php?uid='.$enemy_id ,$date);
				
			}
			
	
			if($spies_kia == 1)
			{
				
				$enemy_spy_killed_msg = $user->lang['ENEMY_SPY_KILLED'];
				$enemy_report = sprintf($user->lang['ENEMY_SPY_FAILURE'],$kingdom_name,$enemy_spy_killed_msg);
				report_kingdom_news($enemy_id , $enemy_report , $date);
				
			}else{
				
				$enemy_spy_killed_msg = sprintf($user->lang['ENEMY_SPIES_KILLED'],$spies_kia);
				$enemy_report = sprintf($user->lang['ENEMY_SPY_FAILURE'],$kingdom_name,$enemy_spy_killed_msg);
				report_kingdom_news($enemy_id , $enemy_report , $date);
				
			}
			
			// Now it is time to redirect the user to see the report page so they can see the report
			redirect(append_sid("{$phpbb_root_path}/games/aod/report.$phpEx"));				
		}else{
			// Failed

						// Update kingdom data
			$db->sql_query(
			"UPDATE " . AOD_KINGDOMS_TABLE ." SET 
			kingdom_spies = kingdom_spies - $spies_kia,
			kingdom_turns = kingdom_turns - 1
			WHERE user_id = $user_id");
			$db->sql_return_on_error(false);

			if($spies_kia == 1)
			{
			
				$spy_killed_msg = $user->lang['SPY_KILLED'];
				$kingdom_report = sprintf($user->lang['SPY_FAILURE_REPORT'],$enemy_name,$spy_killed_msg).$spy_report;
				$report = sprintf($user->lang['UNSUCCESSFUL_SPY_MISSION'],$enemy_name,$spy_killed_msg).$spy_report;
				report_kingdom_news($user_id , $kingdom_report , $date);
				report_transaction($user_id,$report,'viewking.php?uid='.$enemy_id ,$date);
				
			}else{
				
				$spy_killed_msg = sprintf($user->lang['SPIES_KILLED'],$spies_kia);
				$kingdom_report = sprintf($user->lang['SPY_FAILURE_REPORT'],$enemy_name,$spy_killed_msg).$spy_report;
				$report = sprintf($user->lang['UNSUCCESSFUL_SPY_MISSION'],$enemy_name,$spy_killed_msg).$spy_report;
				report_kingdom_news($user_id , $kingdom_report , $date);
				report_transaction($user_id,$report,'viewking.php?uid='.$enemy_id ,$date);
				
			}
			
	
			if($spies_kia == 1)
			{
				
				$enemy_spy_killed_msg = $user->lang['ENEMY_SPY_KILLED'];
				$enemy_report = sprintf($user->lang['ENEMY_SPY_SUCCESS'],$kingdom_name,$enemy_spy_killed_msg);
				report_kingdom_news($enemy_id , $enemy_report , $date);
				
			}else{
				
				$enemy_spy_killed_msg = sprintf($user->lang['ENEMY_SPIES_KILLED'],$spies_kia);
				$enemy_report = sprintf($user->lang['ENEMY_SPY_SUCCESS'],$kingdom_name,$enemy_spy_killed_msg);
				report_kingdom_news($enemy_id , $enemy_report , $date);
				
			}
				
			// Now it is time to redirect the user to see the report page so they can see the report
			redirect(append_sid("{$phpbb_root_path}/games/aod/report.$phpEx"));
		}
		
	}

}

// Display an error messages to the user
if($error_msgs){
	$error_msgs = '<span class="errormsg"><b>'.$error_msgs.'</b></span>';
}

include($phpbb_root_path . 'games/aod/includes/page_header.' . $phpEx);

$template->assign_vars(array(
	'S_MODE'				=> $mode,

	'KINGDOM_ID'			=> $enemy_id,
	'KINGDOM_NAME'			=> $enemy_name.$sponsor_sig,
	'KINGDOM_AVATAR'		=> $enemy_avatar,
	'KINGDOM_AVATAR_WIDTH'	=> $enemy['user_avatar_width'],
	'KINGDOM_RULER'			=> $enemy_ruler,
	'KINGDOM_LEVEL'			=> number_format($enemy_level),
	'KINGDOM_LAND'			=> number_format(fetch_total_land($enemy_id)),
	'KINGDOM_POP'			=> number_format($enemy_pop),
	'KINGDOM_NET'			=> number_format($enemy_net),
	'KINGDOM_WINS'			=> number_format($enemy_wins),
	'KINGDOM_LOSSES'		=> number_format($enemy_losses),
	'COMMAND_BUTTONS'		=> $command_buttons,
	
	'SPIES'					=> $kingdom_spies,
	)
);

// Output page
page_header($game_name.' - '.$user->lang['WAR']);

$template->set_filenames(array(
	'body' => $templates_path.'viewking_body.html')
);

page_footer();
?>